/*
 ***** BEGIN LICENSE BLOCK *****
 * Version: CPL 1.0/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Common Public
 * License Version 1.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.eclipse.org/legal/cpl-v10.html
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
 * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
 * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
 * 
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the CPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the CPL, the GPL or the LGPL.
 ***** END LICENSE BLOCK *****/
package asruby.runtime {

import asruby.Ruby;

import flash.utils.Dictionary;

/**
 * The arity of a method is the number of arguments it takes.
 */
public class Arity {
    private static var serialVersionUID: Number =  1;
    private static var arities: Dictionary =  new Dictionary();
    private var value:int;
    
    public static var NO_ARGUMENTS: Arity =  newArity(0);
    public static var ONE_ARGUMENT: Arity =  newArity(1);
    public static var TWO_ARGUMENTS: Arity =  newArity(2);
    public static var THREE_ARGUMENTS: Arity =  newArity(3);
    public static var OPTIONAL: Arity =  newArity(-1);
    public static var ONE_REQUIRED: Arity =  newArity(-2);
    public static var TWO_REQUIRED: Arity =  newArity(-3);
    public static var THREE_REQUIRED: Arity =  newArity(-3);

    public  function Arity(value:int){
        this.value = value;
    }

    public static function createArity(value:int):Arity{
        switch (value) {
        case -4:
            return THREE_REQUIRED;
        case -3:
            return TWO_REQUIRED;
        case -2:
            return ONE_REQUIRED;
        case -1:
            return OPTIONAL;
        case 0:
            return NO_ARGUMENTS;
        case 1:
            return ONE_ARGUMENT;
        case 2:
            return TWO_ARGUMENTS;
        case 3:
            return THREE_ARGUMENTS;
        }
        return newArity(value);
    }
    
/*     public static function fromAnnotation(anno:JRubyMethod):Arity{
        if (anno.optional() > 0 || anno.rest()) {
            return createArity(-(anno.required() + 1));
        }
        return createArity(anno.required());
    }
    
    public static function fromAnnotation2(anno:JRubyMethod, parameterTypes:Array, isStatic:Boolean):Arity{
        var required: int =  0;
        if (anno.optional() == 0 && !anno.rest() && anno.required() == 0) {
            // try count specific args to determine required
            var i: int =  parameterTypes.length;
            if (isStatic) i--;
            if (parameterTypes.length > 0) {
                if (parameterTypes[0] == ThreadContext) i--;
                if (parameterTypes[parameterTypes.length - 1] == Block) i--;
            }

            required = i;
        } else {
            required = anno.required();
        }
        if (anno.optional() > 0 || anno.rest()) {
            return createArity(-(required + 1));
        }
        return createArity(required);
    } */
    
    private static function newArity(value:int):Arity{
        var integerValue: Number =  value;
        var result:Arity;
        
        result = arities.get(integerValue);
        if (result == null) {
            result = new Arity(value);
            arities.put(integerValue, result);
        }
        
        return result;
    }

    public static function fixed(arity:int):Arity{
        //assert arity >= 0;
        return createArity(arity);
    }

    public static function optional():Arity{
        return OPTIONAL;
    }

    public static function required(minimum:int):Arity{
        //assert minimum >= 0;
        return createArity(-(1 + minimum));
    }

    public static function noArguments():Arity{
        return NO_ARGUMENTS;
    }

    public static function singleArgument():Arity{
        return ONE_ARGUMENT;
    }

    public static function twoArguments():Arity{
        return TWO_ARGUMENTS;
    }
    
/*     public static function procArityOf(node:Node):Arity{
        if (node instanceof AttrAssignNode && node != null) {
            node = ( node).getArgsNode();
        }
        if (node == null) {
            return Arity.optional();
        } else if (node instanceof IArityNode) {
            return ( node).getArity();
        } else if (node instanceof CallNode) {
            return Arity.singleArgument();
        } else if (node instanceof ArrayNode) {
            return Arity.singleArgument();
        }

        throw new Error("unexpected type " + node.getClass() + " at " + node.getPosition());
    } */

    public  function getValue():int{
        return value;
    }

    public  function checkArity(runtime:Ruby, args:Array):void{
        if (isFixed()) {
            if (args.length != required()) {
                throw runtime.newArgumentError("wrong number of arguments(" + args.length + " for " + required() + ")");
            }
        } else {
            if (args.length < required()) {
                throw runtime.newArgumentError("wrong number of arguments(" + args.length + " for " + required() + ")");
            }
        }
    }

    public  function isFixed():Boolean{
        return value >= 0;
    }

    public  function required():int{
        if (value < 0) {
            return -(1 + value);
        }
        return value;
    }

    public  function equals(other:Object):Boolean{
        return this == other;
    }

    public  function hashCode():int{
        return value;
    }

    public  function toString():String{
        if(isFixed()) {
            return "Fixed" + required();
        } else {
            return "Opt";
        }
    }

    // Some helper functions:

    public static function checkArgumentCount(runtime:Ruby, args:Array, min:int, max:int):int{
        if (args.length < min) {
            throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + min + ")");
        }
        if (max > -1 && args.length > max) {
            throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + max + ")");
        }
        return args.length;
    }

    public static function raiseArgumentError(runtime:Ruby, length:int, min:int, max:int):void{
        if (length < min) {
            throw runtime.newArgumentError("wrong number of arguments (" + length + " for " + min + ")");
        }
        if (max > -1 && length > max) {
            throw runtime.newArgumentError("wrong number of arguments (" + length + " for " + max + ")");
        }
    }

    /**
     * @see org.jruby.runtime.builtin.IRubyObject#scanArgs()
     */
    public static function scanArgs(runtime:Ruby, args:Array, required:int, optional:int):Array{
        var total: int =  required+optional;
        var real: int =  checkArgumentCount(runtime, args,required,total);
        var narr: Array =  args.slice(0, real);
        
        for ( var i:int =real; i<total; i++) {
            narr[i] = runtime.getNil();
        }
        return narr;
    }
}
}